package com.yxkj.syh.app.huarong.activities.orders.detail.status_other;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.syh.app.basic.base.BaseResponse;
import com.syh.app.basic.base.view_model.BaseViewModel;
import com.syh.app.basic.utils.DateUtils;
import com.syh.app.basic.utils.Tooast;
import com.syh.app.basic.utils.net.ApiObserver;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.yxkj.syh.app.huarong.bean.CheckInfo;
import com.yxkj.syh.app.huarong.bean.OrderDetail;
import com.yxkj.syh.app.huarong.bean.OrderDetailResponse;
import com.yxkj.syh.app.huarong.bean.OrderProcess;
import com.yxkj.syh.app.huarong.constants.OrderStatus;
import com.yxkj.syh.app.huarong.data_center.model.OrderModel;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import mlxy.utils.S;

/**
 * 创建者：SYH
 * 时间：2020/5/20 13:20
 * 编辑者：SYH、
 * 标签：
 * 补充说明：
 */
public class OrderOtherStatusVM extends BaseViewModel<ActivityEvent> {

    public OrderOtherStatusVM(@NonNull Application application) {
        super(application);
    }


    public MutableLiveData<OrderDetail> mldOrderDetail = new MutableLiveData<>();
    public MutableLiveData<Boolean> mldClear7 = new MutableLiveData<>();

    public String getLogExtendJson(String key, int action, int flag) {
        List<OrderProcess> processes = mldOrderDetail.getValue().getLogs();

        String jsonStr = "";
        for (OrderProcess process : processes) {
            if (process.getAction() == action) {
                if (process.getFlag() == flag) {
                    jsonStr = process.getExtendJson();
                    break;
                }
            }
        }

        if (TextUtils.isEmpty(jsonStr)) {
            return "";
        } else {
            try {
                JSONObject jsonObject = new JSONObject(jsonStr);
                return jsonObject.getString(key);
            } catch (JSONException e) {
                return "";
            }
        }
    }

    public String getLogMsg(int action,int flag) {
        List<OrderProcess> processes = mldOrderDetail.getValue().getLogs();

        for (OrderProcess process : processes) {
            if (process.getAction() == action) {
                if (process.getFlag() == flag) {
                    return  process.getMessage();
                }
            }
        }
        return "--";
    }

    /**
     * 获取时间
     */
    public String getOrderTime(int action){
        List<OrderProcess> processes = mldOrderDetail.getValue().getLogs();

        for (OrderProcess process : processes) {
            if (process.getAction() == action) {
                if (process.getFlag() == 1) {
                    return process.getCreateTime();
                }
            }
        }
        return "--";
    }

    public CheckInfo getCheckInfo(){
        List<OrderProcess> processes = mldOrderDetail.getValue().getLogs();

        for (OrderProcess process : processes) {
            if (process.getAction() == OrderStatus.WAIT_PRICING) {
                if (process.getFlag() == 1) {
                    return new Gson().fromJson(process.getExtendJson(),CheckInfo.class);
                }
            }
        }
        return null;
    }


    public void getOrderDetail(long id) {
        OrderModel.getOrderModel().orderDetail(id, mLifecycleProvider.bindToLifecycle(), new ApiObserver<OrderDetailResponse>() {
            @Override
            public void onSuccess(OrderDetailResponse orderDetailResponse) {
                mldOrderDetail.setValue(orderDetailResponse.getData());
            }

            @Override
            public void onFailed(int code, String msg) {
                Tooast.warning(msg);
            }
        });
    }

    public void clearStatus7(long id) {
        showLoading("clearStatus7");
        OrderModel.getOrderModel().clearStatus7(id, mLifecycleProvider.bindToLifecycle(), new ApiObserver<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                hideLoading("clearStatus7");
                Tooast.success(response.getMessage());
                mldClear7.setValue(true);
            }

            @Override
            public void onFailed(int code, String msg) {
                hideLoading("clearStatus7");
                Tooast.warning(msg);
            }
        });
    }



}
